/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cleanerUI.java
 *
 * Created on 14 juil. 2010, 00:40:15
 */

package jhtmlcleaner;

import core.Actions;
import core.HTMLCleaner;
import core.Setting;
import core.SettingsFinder;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 *
 * @author Victor
 */
public class cleanerUI extends javax.swing.JFrame {

    /** Creates new form cleanerUI */
    public cleanerUI() {
        initComponents();
        optionCheckBoxes=null;
        jFileChooser=new JFileChooser();
        jFileChooser.setCurrentDirectory(new File("."));
        jFileChooser.setMultiSelectionEnabled(false);
        
//        optionCheckBoxes[0]=new jHTMLCheckBoxMenuItem("Correct titles", Actions.CORRECT_TITLES, null,customTitle);
//        optionCheckBoxes[1]=new jHTMLCheckBoxMenuItem("Correct list items", Actions.CORRECT_TAG, "li",customListItem);
//        optionCheckBoxes[2]=(new jHTMLCheckBoxMenuItem("Remove <div> tags", Actions.REMOVE_TAG,"div", null));
//        optionCheckBoxes[3]=(new jHTMLCheckBoxMenuItem("Remove <span> tags", Actions.REMOVE_TAG, "span", null));
//        optionCheckBoxes[4]=(new jHTMLCheckBoxMenuItem("Remove width attribute", Actions.REMOVE_ATTRIBUTE, null, "width"));
//        optionCheckBoxes[5]=(new jHTMLCheckBoxMenuItem("Remove height attribute", Actions.REMOVE_ATTRIBUTE, null, "height"));
//        optionCheckBoxes[6]=(new jHTMLCheckBoxMenuItem("Remove style attribute from td", Actions.REMOVE_ATTRIBUTE_FROM_TAG, "td", "style"));
//        optionCheckBoxes[7]=(new jHTMLCheckBoxMenuItem("Remove style attribute from table", Actions.REMOVE_ATTRIBUTE_FROM_TAG, "table", "style"));
//        optionCheckBoxes[8]=(new jHTMLCheckBoxMenuItem("Remove style attribute from tr", Actions.REMOVE_ATTRIBUTE_FROM_TAG, "tr", "style"));
//        optionCheckBoxes[9]=(new jHTMLCheckBoxMenuItem("Remove font-size attribute from style", Actions.SUPPRESS_ATTRIBUTE_IN_STYLE_ATTRIBUTE, null, "font-size"));
//         optionCheckBoxes[10]=(new jHTMLCheckBoxMenuItem("Remove font-family attribute from style", Actions.SUPPRESS_ATTRIBUTE_IN_STYLE_ATTRIBUTE, null, "font-family"));
//
//        optionCheckBoxes[11]=(new jHTMLCheckBoxMenuItem("Remove class attribute", Actions.REMOVE_ATTRIBUTE, null, "class"));
//        optionCheckBoxes[12]=(new jHTMLCheckBoxMenuItem("Remove rowspan attribute", Actions.REMOVE_ATTRIBUTE, null, "rowspan"));
//        optionCheckBoxes[13]=(new jHTMLCheckBoxMenuItem("Remove <font> tags", Actions.REMOVE_TAG, "font", null));
    
        

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        oldHTMLTextArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        newHTMLTextArea = new javax.swing.JTextArea();
        cleanButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        loadSettingsFileMenuItem = new javax.swing.JMenuItem();
        quitMenuItem = new javax.swing.JMenuItem();
        settingsMenu = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("jHTMLCleaner");

        oldHTMLTextArea.setColumns(20);
        oldHTMLTextArea.setRows(5);
        jScrollPane1.setViewportView(oldHTMLTextArea);

        newHTMLTextArea.setColumns(20);
        newHTMLTextArea.setRows(5);
        jScrollPane2.setViewportView(newHTMLTextArea);

        cleanButton.setText("Clean");
        cleanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cleanButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Old HTML");

        jLabel2.setText("New HTML");

        jMenu1.setText("File");

        loadSettingsFileMenuItem.setText("Load settings file");
        loadSettingsFileMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadSettingsFileMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(loadSettingsFileMenuItem);

        quitMenuItem.setText("Quit");
        quitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(quitMenuItem);

        jMenuBar1.add(jMenu1);

        settingsMenu.setText("Settings");
        jMenuBar1.add(settingsMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cleanButton, javax.swing.GroupLayout.DEFAULT_SIZE, 635, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cleanButton, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cleanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cleanButtonActionPerformed
        // TODO add your handling code here:
        clean();
    }//GEN-LAST:event_cleanButtonActionPerformed

    private void quitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitMenuItemActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_quitMenuItemActionPerformed

    private void loadSettingsFileMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadSettingsFileMenuItemActionPerformed
        // TODO add your handling code here:
        jFileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
        int resultI=jFileChooser.showOpenDialog(this);
        String file=null;
        switch(resultI){
            case JFileChooser.APPROVE_OPTION:
                file=jFileChooser.getSelectedFile().getPath();
                break;
            case JFileChooser.CANCEL_OPTION:
                return;
            case JFileChooser.ERROR_OPTION:
                return;

        }
        System.out.println(file);
        InputSource in=null;
        try{
            SettingsFinder dF=new SettingsFinder();
            XMLReader reader=XMLReaderFactory.createXMLReader();
            reader.setContentHandler(dF);
            reader.setErrorHandler(dF);
            in=new InputSource(file);
            reader.parse(in);
        }
        catch(Exception e){
            System.out.println("Exception\n"+e.getMessage());
            JOptionPane.showMessageDialog(this, "The settings file has a problem.\n"+e.getMessage(), "Error with the settings file.", JOptionPane.ERROR_MESSAGE);
        }
        Setting[] s=Setting.getSettingsByPriority();
        optionCheckBoxes=new jHTMLCheckBoxMenuItem[s.length];
        for (int i=0;i<s.length;i++){
            optionCheckBoxes[i]=new jHTMLCheckBoxMenuItem(this,s[i]);
        }
        settingsMenu.removeAll();
        initOptionCheckBoxes();
    }//GEN-LAST:event_loadSettingsFileMenuItemActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new cleanerUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cleanButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JMenuItem loadSettingsFileMenuItem;
    private javax.swing.JTextArea newHTMLTextArea;
    private javax.swing.JTextArea oldHTMLTextArea;
    private javax.swing.JMenuItem quitMenuItem;
    private javax.swing.JMenu settingsMenu;
    // End of variables declaration//GEN-END:variables

    private jHTMLCheckBoxMenuItem[] optionCheckBoxes;
    private HTMLCleaner cleaner;
    private JFileChooser jFileChooser;
    private void clean(){
        if(!(optionCheckBoxes==null)){
            cleaner=new HTMLCleaner(oldHTMLTextArea.getText());
            for (int i=0;i<optionCheckBoxes.length;i++){
                optionCheckBoxes[i].execute(cleaner);
            }
            newHTMLTextArea.setText(cleaner.getDocument());
        }
        else JOptionPane.showMessageDialog(this, "A settings file must be loaded before cleaning the text.","File not loaded", JOptionPane.WARNING_MESSAGE);
    }
    private void initOptionCheckBoxes(){
        for (int i=0;i<optionCheckBoxes.length;i++){
            settingsMenu.add(optionCheckBoxes[i]);
            optionCheckBoxes[i].setEnabled(true);
            optionCheckBoxes[i].setToolTipText(optionCheckBoxes[i].getSetting().getType().getHelp());
        }
        
    }
}
